unit clsNao_Conformidade;

interface

uses biblio,dm,dbtables;

type TVetor_NCF = array of string;
type TNao_Conformidade =class(TObject)
     public
      descricao_ncf: string;
      function  Gravar   (p_cod_ncf: string):byte;
      function  Recuperar(p_cod_ncf : string):byte;
      function  Recuperar_PorDescricao(Descricao : string): string;
      procedure Recuperar_Todos(var vetor: TVetor_NCF);
      function  Excluir  (P_cod_ncf : string):byte;
      procedure Consultar(P_q:TQuery;P_descricao_ncf: string);
     end;

implementation

procedure TNao_Conformidade.Consultar(P_q: TQuery;
  P_descricao_ncf: string);
begin
  P_q.sql.clear;
  P_q.Params.clear;
  P_q.sql.Add('Select * from nao_conformidade where descricao_ncf>=:k_descricao_ncf order by descricao_ncf');
  P_q.ParamByName('K_descricao_ncf').AsString := P_descricao_ncf;
end;

function TNao_Conformidade.Excluir(P_cod_ncf: string): byte;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.Params.clear;
  tq.sql.clear;
  tq.sql.add('delete from Nao_conformidade where cod_ncf=:K_cod_Ncf');
  tq.ParamByName('K_cod_ncf').AsString := p_cod_ncf;
  Result := 1;
  tq.ExecSQL;
  tq.close;
  tq.free;
end;

function TNao_Conformidade.Gravar(p_cod_ncf: string): byte;
var
  tq: TQuery;
begin
  tq := TQuery.create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.Params.clear;
  tq.sql.clear;
  tq.sql.add('Select * from Nao_conformidade where cod_ncf=:K_cod_Ncf');
  tq.ParamByName('K_cod_ncf').AsString := p_cod_ncf;
  tq.open;
  if tq.eof then begin
    tq.sql.clear;
    tq.Params.clear;
    tq.sql.add('insert into nao_conformidade values(:K_cod_ncf,:K_descricao_ncf)')
  end else begin
    tq.sql.clear;
    tq.Params.clear;
    tq.sql.add('Update nao_conformidade set descricao_ncf=:K_descricao_ncf '+
               'where cod_ncf=:K_cod_ncf');
  end;
  tq.ParamByName('K_cod_ncf').AsString      := strzero(p_cod_ncf,2);
  tq.ParamByName('K_descricao_ncf').AsString:= descricao_ncf;
  tq.ExecSQL;
  tq.Close;
  tq.free;
  result:= 1;
end;

function TNao_Conformidade.Recuperar(p_cod_ncf: string): byte;
var
  tq : TQuery;
begin
  tq := TQuery.create(DmInt);
  tq.DatabaseName := DmInt.Db.DatabaseName;
  tq.Active := false;
  tq.Params.clear;
  tq.sql.clear;
  tq.sql.add('Select * from Nao_conformidade where cod_ncf=:K_cod_Ncf');
  tq.ParamByName('K_cod_ncf').AsString := p_cod_ncf;
  tq.open;
  if not tq.eof then
    Result := 1
  else
    Result := 0;
  descricao_ncf := tq.fieldbyname('descricao_ncf').AsString;
  tq.close;
  tq.free;
end;

function TNao_Conformidade.Recuperar_PorDescricao;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.Active      := false;
 tq.Params.clear;
 tq.sql.clear;
 tq.sql.add('Select cod_ncf from Nao_conformidade where descricao_ncf=:a');
 tq.params[0].AsString:= Descricao;
 tq.open;
 result:= tq.fieldbyname('cod_ncf').AsString;
 tq.close;
 tq.free;
end;

procedure TNao_Conformidade.Recuperar_Todos;
var tq: TQuery;
begin
 tq             := TQuery.create(DmInt);
 tq.DatabaseName:= DmInt.Db.DatabaseName;
 tq.Active      := false;
 tq.Params.clear;
 tq.sql.clear;
 tq.sql.add('Select descricao_ncf from Nao_conformidade');
 tq.open;
 setlength(vetor,0);
 while not tq.eof do begin
  setlength(vetor,length(vetor)+1);
  vetor[length(vetor)-1]:= tq.fieldbyname('descricao_ncf').AsString;
  tq.next;
 end;
 tq.close;
 tq.free;
end;

end.
